﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace Spotted.Views
{
    public partial class share : PhoneApplicationPage
    {

        private App app = Application.Current as App;
     
        EmailAddressChooserTask emailAddresstask;// Constructor


        public share()
        {
            InitializeComponent();
            app.navigatedFromShare = true;
            this.emailAddresstask = new EmailAddressChooserTask();
            this.emailAddresstask.Completed += new EventHandler<EmailResult>(emailAddresstask_Completed);
            
        }




        void emailAddresstask_Completed(object sender, EmailResult e)
        {
            if(e.TaskResult == TaskResult.OK)
            {
                address_email_Box.Text = e.Email;
                
            }
           
        }

        //private void email_contacts_button_Click(object sender, RoutedEventArgs e)
        //{
        //    emailAddresstask.Show();
        //}

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var lastPage = NavigationService.BackStack.FirstOrDefault();

            if(lastPage != null && lastPage.Source.ToString() == "/Spotted;component/Views/newPinSpotPage.xaml")
            {
                chooseSpot.Visibility = System.Windows.Visibility.Collapsed;
                email_canvas.Visibility = System.Windows.Visibility.Visible;
                
            }

            else if(lastPage != null && lastPage.Source.ToString() == "/MainPage.xaml")
            {
                if(app.Logged_User.UserSpots.Count == 0)
                {
                    MessageBoxResult m = MessageBox.Show("Oops... It seems that you don't have any images to share. Please click 'New Spot' to add first image.", "", MessageBoxButton.OK);
                    if(m == MessageBoxResult.OK)
                    {
                        this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                    }
                }
                else
                {
                    chooseSpot.Visibility = System.Windows.Visibility.Visible;
                }
            }

            else
            {
                email_canvas.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

      
       

        private void address_email_Box_TextChanged(object sender, TextChangedEventArgs e)
        {
           
        }

        private void subject_email_Box_TextChanged(object sender, TextChangedEventArgs e)
        {
          
        }

        private void body_email_Box_TextChanged(object sender, TextChangedEventArgs e)
        {
            
        }

    
        
        private void send_email_Click(object sender, RoutedEventArgs e)
        {
            body_email_Box.Text += "\n";
            foreach(var url in app.SahredImages)
            {
                body_email_Box.Text += "\n" + url;
            }


            EmailComposeTask Myemail_Composetask = new EmailComposeTask();
            Myemail_Composetask.To = address_email_Box.Text;

            Myemail_Composetask.Subject = subject_email_Box.Text;
            Myemail_Composetask.Body = body_email_Box.Text;
            Myemail_Composetask.Show();

            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void cancel_email_Click(object sender, RoutedEventArgs e)
        {
            address_email_Box.Text = "";
            subject_email_Box.Text = "";
            body_email_Box.Text = "";
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

      
    }
}